﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using AthCommander.Model;
using System.Threading.Tasks;
using AthCommander.Infrastructure;

namespace AthCommander.DirectoryGrid.Dialogs
{
	public partial class CopyDialog : Form
	{
		private FileView _file;

		public CopyDialog(FileView file)
		{
			InitializeComponent();
			
			_file = file;
			newPath.Text = _file.FullName;
		}

		#region Private Helpers

		private void Copy()
		{
			DirectoryInfo root = Directory.GetParent(_file.FullName);
			string destinPath = Path.Combine(root.FullName, newPath.Text);
            CopyHelper helper = new CopyHelper(copyProgres);
			if (_file.IsDirectory)
			{
				DirectoryInfo newInfo = new DirectoryInfo(destinPath);
				DirectoryInfo oldInfo = new DirectoryInfo(_file.FullName);

				helper.CopyDirectory(oldInfo, newInfo);
			}
			else
			{
                FileInfo newInfo = new FileInfo(destinPath);
                FileInfo oldInfo = new FileInfo(_file.FullName);
				helper.CopyFile(oldInfo, newInfo, FileMode.Create);
			}

			this.Close();
		}

        private bool Validate(string newPath)
        {
            if (File.Exists(newPath) || Directory.Exists(newPath))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

		#endregion

		#region Event Handlers

		private void copyButton_Click(object sender, EventArgs e)
		{
            if (Validate(newPath.Text))
            {
                this.Cursor = Cursors.WaitCursor;
                this.copyButton.Enabled = this.cancelButton.Enabled = false;
                Copy();
            }
            else
            {
                MessageBox.Show("Path is invalid. Correct errors.");
            }
		}

		private void cancelButton_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		#endregion
	}
}
